<html>
<head>
<title>Cash Register</title>
<TMPL_INCLUDE NAME=styles.tmpl.html>
<TMPL_INCLUDE NAME=javascript.tmpl.html>
<TMPL_INCLUDE NAME=tracking.tmpl.html>
<script type="text/javascript"  src="gears_init.js"></script>
<script type="text/javascript" src="sample.js"></script>
</head>
<body>
<TMPL_INCLUDE name=menubar.tmpl.html>
<!-- begin body -->
<div id="doc">
<h1>Cash Register - Cashier </h1>
cashier:<TMPL_VAR NAME=hijinx_name><br>
<TMPL_INCLUDE name=session.tmpl.html>

<form onsubmit="handleSubmit(); return false;">
  <b>Enter or scan a product code:</b>&nbsp;<br>
  <table>
    <tr>
      <td valign="middle">
	code<input type="text" id="code" style="width:20em;"> 
	<!-- <input type="text" id="submitValue" style="width:20em;"><br> -->
      <input type="submit" value="OK"> 
      </td>
    </tr>
  </table>
</form>

<p><b>results:</b>

<p><span id="status">&nbsp;</span>

<p><i>This page uses Google Gears to record your entries on the local disk.
   If you navigate away and revisit this page, all your data will still
   be here.  Try it!</i>


<form onsubmit="handleNewSubmit(); return false;">
  <b>Enter a product to store in the database:</b>&nbsp;<br>
  <table>
    <tr>
      <td valign="middle">
	desc<input type="text" id="desc" style="width:20em;"><br>
	price<input type="text" id="price" style="width:10em;"><br>
	code<input type="text" id="code" style="width:20em;"><br>
	<!-- <input type="text" id="submitValue" style="width:20em;"><br> -->
      <input type="submit" value="OK"> 
      </td>
    </tr>
  </table>
</form>

<p><b>inventory:</b>

<p><span id="status">&nbsp;</span>
<p><span id="error">&nbsp;</span>

</div>




<!-- end body -->
<script>

var db;
init();

// Open this page's local database.
function init() {
  if (!window.google || !google.gears) {
    return;
  }

  try {
    db = google.gears.factory.create('beta.database', '1.0');
  } catch (ex) {
    setError('Could not create database: ' + ex.message);
  }

  if (db) {
    db.open('hijinx_register');
    db.execute('create table if not exists Products' +
               ' (Desc varchar(255),  upc varchar(20) unique, price real)');
    // Initialize the UI at startup.
    displayRecentPhrases();
  }else{
	alert("google gears not working")
  }

  // Enable or disable UI elements
  var init_succeeded = !!db;
  var inputs = document.getElementsByTagName('input');
  for (var i = 0, el; el = inputs[i]; i++) {
    el.disabled = !init_succeeded;
  }

}

function handleSubmit() {
  if (!google.gears.factory || !db) {
    return;
  }

  var elm3 = document.getElementById('code');
  var code = elm3.value;
  var currTime = new Date().getTime();

  // Insert the new item.
  // The Gears database automatically escapes/unescapes inserted values.

  // Update the UI.
  elm3.value = '';
  displayRecentPhrases(code);
}


function displayRecentPhrases(code) {
  var recentPhrases = [];

  // We re-throw Gears exceptions to make them play nice with certain tools.
  // This will be unnecessary in a future version of Gears.
  try {

    // Get the 3 most recent entries. Delete any others.
    var rs = db.execute('select * from Products where upc = ? ', [code]);
    var index = 0;
    while (rs.isValidRow()) {
        recentPhrases[index] = rs.field(0) + " $" + rs.field(2) + " " + rs.field(1);
      ++index;
      rs.next();
    }
    rs.close();

  } catch (e) {
    throw new Error(e.message);
  }

  var status = document.getElementById('status');
  status.innerHTML = '';
  for (var i = 0; i < recentPhrases.length; ++i) {
    var bullet = '(' + (i + 1) + ') ';
    status.appendChild(document.createTextNode(bullet + recentPhrases[i]));
    status.appendChild(document.createElement('br'));
  }
}
function handleNewSubmit() {
  if (!google.gears.factory || !db) {
    return;
  }

  var elm1 = document.getElementById('desc');
  var desc = elm1.value;
  var elm2 = document.getElementById('price');
  var price = elm2.value;
  var elm3 = document.getElementById('code');
  var code = elm3.value;
  var currTime = new Date().getTime();

  // Insert the new item.
  // The Gears database automatically escapes/unescapes inserted values.
  try {
  	db.execute('insert into Products values (?, ?, ?)', [desc,code,price]);
  } catch (ex) {
    setError('Could not insert item: ' + ex.message);
  }

  // Update the UI.
  elm.value = '';
  displayRecentPhrases();
}
function displayRecentPhrases() {
  var recentPhrases = [];

  // We re-throw Gears exceptions to make them play nice with certain tools.
  // This will be unnecessary in a future version of Gears.
  try {

    // Get the 3 most recent entries. Delete any others.
    var rs = db.execute('select * from Products ');
    var index = 0;
    while (rs.isValidRow()) {
        recentPhrases[index] = rs.field(0) + " $" + rs.field(2) + " " + rs.field(1);
      ++index;
      rs.next();
    }
    rs.close();

  } catch (e) {
    throw new Error(e.message);
  }

  var status = document.getElementById('status');
  status.innerHTML = '';
  for (var i = 0; i < recentPhrases.length; ++i) {
    var bullet = '(' + (i + 1) + ') ';
    status.appendChild(document.createTextNode(bullet + recentPhrases[i]));
    status.appendChild(document.createElement('br'));
  }
}

</script>
</body>
</html>
